package com.cmwa.solution.business.register.controller;

import java.io.FileInputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.register.entity.CustInfoVo;
import com.cmwa.solution.business.register.entity.HomePageDataVo;
import com.cmwa.solution.business.register.service.SalesDataCheckService;

@Controller
@RequestMapping("/business/custRegister")
public class CustEquityRegisterController {
	
	@Autowired
	private SalesDataCheckService salesDataCheckService;
	
	@RequestMapping("/home")
	public ModelAndView home(ModelAndView modelAndView) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sdf.format(date);
		modelAndView.addObject("date",dateStr);
		modelAndView.setViewName("business/register/home");
		return modelAndView;
	}
	
	@RequestMapping("/registerHomePage")
	public String registerHomePage() {
		return "business/register/registerHomePage";
	}
	
	@RequestMapping("/getEchartsData")
	@ResponseBody
	public Object getEchartsData(String type) {
		Map<String,String> dataMap = new HashMap<String,String>();
		SearchParam sp1 = new SearchParam();
		SearchParam sp2 = new SearchParam();
		SearchParam sp3 = new SearchParam();
		SearchParam sp4 = new SearchParam();
		sp1.getSp().put("dataType", "0");
		sp2.getSp().put("dataType", "1");
		sp3.getSp().put("dataType", "2");
		sp4.getSp().put("dataType", "3");
		
		if("2".equals(type)) {
			
			sp1.getSp().put("dateCate", "1");
			sp2.getSp().put("dateCate", "1");
			sp3.getSp().put("dateCate", "1");
			sp4.getSp().put("dateCate", "1");
			
		}else if("7".equals(type)) {
			sp1.getSp().put("dateCate", "0");
			sp2.getSp().put("dateCate", "0");
			sp3.getSp().put("dateCate", "0");
			sp4.getSp().put("dateCate", "0");
		}
		HomePageDataVo capVo = salesDataCheckService.queryHomePageData(sp1);
		HomePageDataVo custVo = salesDataCheckService.queryHomePageData(sp2);
		HomePageDataVo orderVo = salesDataCheckService.queryHomePageData(sp3);
		HomePageDataVo capInVo = salesDataCheckService.queryHomePageData(sp4);
		capVo = capVo == null?new HomePageDataVo():capVo;
		custVo = custVo == null?new HomePageDataVo():custVo;
		orderVo = orderVo == null?new HomePageDataVo():orderVo;
		capInVo = capInVo == null?new HomePageDataVo():capInVo;
		if("2".equals(type)) {
			dataMap.put("crAmount1", capVo.getMonData());
			dataMap.put("crAmount2", capVo.getTueData());
			dataMap.put("crAmount3", capVo.getWedData());
			dataMap.put("crAmount4", capVo.getThuData());
			dataMap.put("crAmount5", capVo.getFriData());
			
			dataMap.put("rAmount1", capInVo.getMonData());
			dataMap.put("rAmount2", capInVo.getTueData());
			dataMap.put("rAmount3", capInVo.getWedData());
			dataMap.put("rAmount4", capInVo.getThuData());
			dataMap.put("rAmount5", capInVo.getFriData());
			
			dataMap.put("custOverView1", custVo.getMonData());
			dataMap.put("custOverView2", custVo.getTueData());
			dataMap.put("custOverView3", custVo.getWedData());
			dataMap.put("custOverView4", custVo.getThuData());
			dataMap.put("custOverView5", custVo.getFriData());
			
			dataMap.put("orderOverView1", orderVo.getMonData());
			dataMap.put("orderOverView2", orderVo.getTueData());
			dataMap.put("orderOverView3", orderVo.getWedData());
			dataMap.put("orderOverView4", orderVo.getThuData());
			dataMap.put("orderOverView5", orderVo.getFriData());
		}else if("7".equals(type)) {
			dataMap.put("crAmount1", capVo.getFirMonData());
			dataMap.put("crAmount2", capVo.getSecMonData());
			dataMap.put("crAmount3", capVo.getThiMonData());
			
			dataMap.put("rAmount1", capInVo.getFirMonData());
			dataMap.put("rAmount2", capInVo.getSecMonData());
			dataMap.put("rAmount3", capInVo.getThiMonData());
			
			dataMap.put("custOverView1", custVo.getFirMonData());
			dataMap.put("custOverView2", custVo.getSecMonData());
			dataMap.put("custOverView3", custVo.getThiMonData());
			
			dataMap.put("orderOverView1", orderVo.getFirMonData());
			dataMap.put("orderOverView2", orderVo.getSecMonData());
			dataMap.put("orderOverView3", orderVo.getThiMonData());
		}
		
		
		
		return dataMap;
	}
	
	@RequestMapping("/downloadFile")
	/*@ResponseBody*/
	public void downloadFile(SearchParam sp,HttpServletResponse response) {
		Map<String,String> map = new HashMap<String,String>();
		OutputStream out = null;
		try {
			response.reset();
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/octet-stream;charset=UTF-8");
			if("0".equals(sp.getSp().get("fileType"))) {
				response.setHeader("Content-Disposition",
						"Attachment;filename="+new String("当日客户数据".getBytes("gb2312"), "ISO8859-1")+".zip");
			}else if("1".equals(sp.getSp().get("fileType"))){
				response.setHeader("Content-Disposition",
						"Attachment;filename="+new String("当日份额与交易数据".getBytes("gb2312"), "ISO8859-1")+".zip");
			}
			out = response.getOutputStream();
			custRegisterBatchDown(sp,out);
			out.close();
			map.put("errCode", "0000");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("errCode", "9999");
		}
		/*return map;*/
	}
	
	public void custRegisterBatchDown(SearchParam sp, OutputStream out) throws Exception {
		List<CustInfoVo> attList = salesDataCheckService.queryCustInfoList("");
		String type=(String) sp.getSp().get("fileType");
		String fileName = "0".equals(type)?"客户数据" : "份额与交易数据" ;
		if(CollectionUtils.isEmpty(attList)) {
			return;
		}
		Map<String,List<CustInfoVo>> map = new HashMap<String,List<CustInfoVo>>();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String dateStr = sdf.format(date);
		int num = 0;
		for(CustInfoVo custInfoVo:attList) {
			num++;
			String prdCode = dateStr + "000" + num;
			//特殊处理为空数据
			if(StringUtils.isBlank(prdCode)) {
				prdCode = "-";
			}
			if(map.containsKey(prdCode)) {
				map.get(prdCode).add(custInfoVo);
				map.put(prdCode, map.get(prdCode));
			}else {
				List<CustInfoVo> list = new ArrayList<CustInfoVo>();
				list.add(custInfoVo);
				map.put(prdCode, list);
			}
		}
		ZipOutputStream outZip = new ZipOutputStream(out);
		try {
			for (Map.Entry<String,List<CustInfoVo>> entry : map.entrySet()) {
				List<CustInfoVo> list = entry.getValue();
				if(CollectionUtils.isNotEmpty(list)) {
					for (int i = 0; i < list.size(); i++) {
						outZip.putNextEntry(new ZipEntry(entry.getKey()+"/"+fileName));
						outZip.setEncoding("UTF-8");
						FileInputStream fis1 = null;
						try {
							
							/*fis1 = new FileInputStream(filePath);
							if(fis1!=null){
								byte[] buffer1 = new byte[1024];
								int byteread1 = 0;
								// 写入zip包
								while ((byteread1 = fis1.read(buffer1)) != -1) {
									outZip.write(buffer1, 0, byteread1);
								}
							}*/
						} catch (Exception e) {
							e.printStackTrace();
						}finally{
							if(fis1!=null){
								fis1.close();
							}
						}
					}
				}
			}
		} catch (Exception ex2) {
			ex2.printStackTrace();
			throw new Exception("关闭大对象异常");
		}finally{
			if (outZip != null) {
				outZip.flush();
				outZip.close();
			}
		}
	}
}
